<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/12
 * Time: 9:26
 */

namespace app\mobile\controller;


use think\Db;
use think\Session;

class Pdetail
{
    // 详情页
    public function detail($id)
    {
        // 通过 问题id 查询 问题 用户 评论
        $userInf = Db::table(['zz_user'=>'u','zz_problem'=>'p'])
                    ->where('p.zz_problem_id',$id)
                    ->where('u.zz_id = p.zz_uid')
                    ->find();
        // 通过 用户id 查询 用户共有多少问题
        $problemNum = count(Db::table('zz_problem')->where('zz_uid',$userInf['zz_id'])->select());
        // 通过问题id 查询当前问题下的所有评论 以及评论用户的信息
        $problemComment = Db::table(['zz_comment'=>'c','zz_user'=>'u'])
                    ->where('c.zz_comment_uid = u.zz_id')
                    ->where('c.zz_problem_id',$id)
                    ->select();
        return view('pdetail/detail',[
            'userInf'        => $userInf,
            'problemNum'     => $problemNum,
            'problemComment' => $problemComment,
            'id'             => $id
        ]);
    }

    // 详情页评论处理
    public function dopcontent()
    {
        // 接收用户信息 判断用户是否登录
        $userInf = Session::get('puser');

        if (empty($userInf)){
            $res['status'] = 0;
            return json($res);
        } else {
            $content = input('content');
            $pid = input('problemId');
            if (empty($content)){
                $res['status'] = 2;
                return json($res);
            }
            // 处理数据
            $data = [];
            $data['zz_problem_id'] = $pid;
            $data['zz_comment_uid'] = $userInf['zz_id'];
            $data['zz_comment_content'] = $content;
            $data['zz_comment_time'] = date('Y-m-d');
            // 将数据加入数据库
            Db::table('zz_comment')->insert($data);
            // 返回数据
            $res['status'] = 1;
            return json($res);
        }
    }
}